package com.lsht.system.service.impl.siwei;

import com.lsht.common.config.LshtConfig;
import com.lsht.common.utils.FileUploadTool;
import com.lsht.common.utils.StringUtils;
import com.lsht.common.utils.uuid.IdUtils;
import com.lsht.system.domain.siwei.SiweiImg;
import com.lsht.system.domain.siwei.SiweiPoint;
import com.lsht.system.mapper.siwei.SiweiImgMapper;
import com.lsht.system.mapper.siwei.SiweiPointMapper;
import com.lsht.system.service.siwei.SiweiImgService;
import com.lsht.system.util.NullUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Date;
import java.util.List;

/**
 * 图片管理Service业务层处理
 *
 * @author zyc
 * @date 2024-08-14
 */
@Service
public class SiweiImgServiceImpl implements SiweiImgService {
    @Autowired
    private SiweiImgMapper siweiImgMapper;

    @Autowired
    private SiweiPointMapper siweiPointMapper;

    /**
     * 新增图片管理
     *
     * @param siweiImg 图片管理
     * @param file
     * @return 结果
     */
    @Override
    public int genAdd(SiweiImg siweiImg, MultipartFile file) {
        siweiImg.setCreateTime(new Date());
        final String logoUrl = LshtConfig.getProfile() + File.separator + "siweiImg" + File.separator;
        if (NullUtil.isNotEmpty(file)) {
            //设置文件路径
            String originalFilename = file.getOriginalFilename();
            // 上传并返回新文件名称
            try {
                String fileName = FileUploadTool.upload(logoUrl, file, originalFilename);
            } catch (IOException e) {
                e.printStackTrace();
            }
            String replace = logoUrl.replace(LshtConfig.getProfile(), "/profile") + originalFilename;
            siweiImg.setImgUrl(replace.replaceAll("\\\\", "/"));
        }
        //id 不为空走修改
        if (NullUtil.isNotEmpty(siweiImg.getId())) {
            siweiImg.setUpdateTime(new Date());
            //若用户是admin 则设置userId为null(因为用户id也跟着修改了,变成1的话  用户就无法与地图匹配了)
            return siweiImgMapper.genUpdateById(siweiImg);
        }
        siweiImg.setId(IdUtils.simpleUUID());
        return siweiImgMapper.genAdd(siweiImg);
    }

    /**
     * 新增图片管理app端使用
     *
     * @param siweiImg
     * @return
     */
    @Override
    public int addImgByApp(SiweiImg siweiImg) {
        siweiImg.setCreateTime(new Date());
        try {
            String base64Image = siweiImg.getImage();
            if (StringUtils.isNotEmpty(base64Image)) {

                String imageData = base64Image.split(",")[1]; // Remove data URL prefix
                byte[] decodedBytes = Base64.getDecoder().decode(imageData);
                String fileName = LshtConfig.getProfile() + File.separator + "siweiImg" + File.separator + System.currentTimeMillis() + ".png";
                String directoryPath = LshtConfig.getProfile() + File.separator + "siweiImg";
                File directory = new File(directoryPath);
                if (!directory.exists()) {
                    directory.mkdirs(); // 创建目录
                }
                File file = new File(fileName);
                try (FileOutputStream fos = new FileOutputStream(file)) {
                    fos.write(decodedBytes);
                }
                String replace = fileName.replace(LshtConfig.getProfile(), "/profile");
                siweiImg.setImgUrl(replace.replaceAll("\\\\", "/"));
                //id 不为空走修改
                if (NullUtil.isNotEmpty(siweiImg.getId())) {
                    siweiImg.setUpdateTime(new Date());
                    return siweiImgMapper.genUpdateById(siweiImg);
                }

            }
        } catch (IOException e) {
            System.out.println(e.getMessage());
//            return ResponseEntity.status(500).body("Error saving image");
        }
        siweiImg.setId(IdUtils.simpleUUID());
        return siweiImgMapper.genAdd(siweiImg);
    }

    /**
     * 删除图片管理信息
     *
     * @param id 图片管理主键
     * @return 结果
     */
    @Override
    public int genDeleteById(List<String> idList) {
        return siweiImgMapper.genDeleteById(idList);
    }

    /**
     * 修改图片管理
     *
     * @param siweiImg 图片管理
     * @return 结果
     */
    @Override
    public int genUpdateById(SiweiImg siweiImg) {
        return siweiImgMapper.genUpdateById(siweiImg);
    }

    /**
     * 查询图片管理列表
     *
     * @param siweiImg 图片管理
     * @param deptId
     * @return 图片管理
     */
    @Override
    public List<SiweiImg> genQueryList(SiweiImg siweiImg, Long deptId) {
        //先去查询
        List<String> date = siweiImg.getDate();
        if (NullUtil.isNotNull(date) && date.size() > 0) {
            siweiImg.setBeginTime(date.get(0));
            siweiImg.setEndTime(date.get(1));
        }
        return siweiImgMapper.genQueryList(siweiImg);
    }

    @Override
    public List<SiweiImg> notPageGenQueryList(SiweiImg siweiImg, Long deptId) {
        SiweiPoint siweiPoint = new SiweiPoint();
        List<Long> objects = new ArrayList<>();
        objects.add(deptId);
        if (NullUtil.isNull(deptId)) {
            siweiPoint.setDeptIdList(null);
        } else {
            siweiPoint.setDeptIdList(objects);
        }
        //先去查询  点位状态为显示的数据
        List<String> iconId = siweiPointMapper.selectPointShowHidden(siweiPoint);
        return siweiImgMapper.notPageGenQueryList(iconId);
    }

    /**
     * 根据id查询图片详情信息
     *
     * @param siweiImg
     * @return
     */
    @Override
    public SiweiImg genQueryById(SiweiImg siweiImg) {
        return siweiImgMapper.genQueryById(siweiImg);
    }
}
